package com.ptadmin.report.mws;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.amazonaws.mws.MarketplaceWebServiceException;
import com.amazonaws.mws.model.GetReportRequestListRequest;
import com.amazonaws.mws.model.GetReportRequestListResponse;
import com.amazonaws.mws.model.GetReportRequestListResult;
import com.amazonaws.mws.model.IdList;
import com.amazonaws.mws.model.ReportRequestInfo;
import com.amazonaws.mws.model.ResponseMetadata;
import com.amazonaws.mws.model.TypeList;
import com.ptadmin.report.InputParameter;
import com.ptadmin.report.ReportApplication;

public class GetReportRequestListWrapper extends ReportWrapperBase {

	private static final Logger logger = LoggerFactory.getLogger(GetReportRequestListWrapper.class);
	
	private GetReportRequestListRequest request;
	private GetReportRequestListResponse response;
	
	private Map<String, String> doneReportIdList = new HashMap<String, String>();
	private Map<String, String> doneWithNoDataReportIdList = new HashMap<String, String>();
	
	public void buildRequest(String merchantId, String mwsAuthToken, IdList reportRequestIdList, TypeList reportTypeList) {

		this.request = new GetReportRequestListRequest()
				.withMerchant(merchantId)
				.withMWSAuthToken(mwsAuthToken);
//				.withReportRequestIdList(reportRequestIdList)
//				.withReportTypeList(reportTypeList);
		
		if (reportTypeList != null) {
			request.setReportTypeList(reportTypeList);
		}
		
		if (reportRequestIdList != null) {
			request.setReportRequestIdList(reportRequestIdList);
		}
	}

	public void invokeRequest() {
		try {
			this.response = service.getReportRequestList(request);
			this.getReportIdWithProcessingStatusDone();
		} catch (MarketplaceWebServiceException ex) {
			System.out.println("Caught Exception: " + ex.getMessage());
			System.out.println("Response Status Code: " + ex.getStatusCode());
			System.out.println("Error Code: " + ex.getErrorCode());
			System.out.println("Error Type: " + ex.getErrorType());
			System.out.println("Request ID: " + ex.getRequestId());
			System.out.print("XML: " + ex.getXML());
			System.out.println("ResponseHeaderMetadata: " + ex.getResponseHeaderMetadata());
		} catch (Exception ex) {
			System.out.print(ex.toString());
		}
	}

//	public boolean getAllProcessingStatus(String status) {
//		GetReportRequestListResponse resp = (GetReportRequestListResponse) this.response;
//
//		boolean isEequalStatus = true;
//		List<ReportRequestInfo> reportRequestInfoList = resp.getGetReportRequestListResult().getReportRequestInfoList();
//		for (ReportRequestInfo reportRequestInfo : reportRequestInfoList) {
//			isEequalStatus &= reportRequestInfo.getReportProcessingStatus().equals(status); // _DONE_NO_DATA_
//		}
//		return isEequalStatus;
//	}
	public Map<String, String> getReportIdWithDone() {
		return this.doneReportIdList;
	}
	
	public Map<String, String> getReportIdWithNoDataDone() {
		return this.doneWithNoDataReportIdList;
	}
	
	private void getReportIdWithProcessingStatusDone() {
		GetReportRequestListResponse resp = (GetReportRequestListResponse) this.response;

		List<ReportRequestInfo> reportRequestInfoList = resp.getGetReportRequestListResult().getReportRequestInfoList();
		for (ReportRequestInfo reportRequestInfo : reportRequestInfoList) {
			if (reportRequestInfo.getReportProcessingStatus().equals("_DONE_")) {
				if (reportRequestInfo.isSetGeneratedReportId()) {
//					this.doneReportIdList.add(reportRequestInfo.getGeneratedReportId());
					this.doneReportIdList.put(reportRequestInfo.getGeneratedReportId(), reportRequestInfo.getReportType());
//					reportRequestInfo.getReportType()
				} else {
					logger.warn("report request retuen status _DONE_, but no reportId is generated...");
				}
			} else if (reportRequestInfo.getReportProcessingStatus().equals("_DONE_NO_DATA_")) {
//				this.doneWithNoDataReportIdList.add(reportRequestInfo.getGeneratedReportId());
				this.doneWithNoDataReportIdList.put(reportRequestInfo.getGeneratedReportId(), reportRequestInfo.getReportType());
			}
		}
	}

	public void printResponse() {
		System.out.println("GetReportRequestList Action Response");
		System.out.println("=============================================================================");
		System.out.println();

		System.out.print("    GetReportRequestListResponse");
		System.out.println();
		if (response.isSetGetReportRequestListResult()) {
			System.out.print("        GetReportRequestListResult");
			System.out.println();
			GetReportRequestListResult getReportRequestListResult = response.getGetReportRequestListResult();
			if (getReportRequestListResult.isSetNextToken()) {
				System.out.print("            NextToken");
				System.out.println();
				System.out.print("                " + getReportRequestListResult.getNextToken());
				System.out.println();
			}
			if (getReportRequestListResult.isSetHasNext()) {
				System.out.print("            HasNext");
				System.out.println();
				System.out.print("                " + getReportRequestListResult.isHasNext());
				System.out.println();
			}
			java.util.List<ReportRequestInfo> reportRequestInfoList = getReportRequestListResult
					.getReportRequestInfoList();
			for (ReportRequestInfo reportRequestInfo : reportRequestInfoList) {
				System.out.print("            ReportRequestInfo");
				System.out.println();
				if (reportRequestInfo.isSetReportRequestId()) {
					System.out.print("                ReportRequestId");
					System.out.println();
					System.out.print("                    " + reportRequestInfo.getReportRequestId());
					System.out.println();
				}
				if (reportRequestInfo.isSetReportType()) {
					System.out.print("                ReportType");
					System.out.println();
					System.out.print("                    " + reportRequestInfo.getReportType());
					System.out.println();
				}
				if (reportRequestInfo.isSetStartDate()) {
					System.out.print("                StartDate");
					System.out.println();
					System.out.print("                    " + reportRequestInfo.getStartDate());
					System.out.println();
				}
				if (reportRequestInfo.isSetEndDate()) {
					System.out.print("                EndDate");
					System.out.println();
					System.out.print("                    " + reportRequestInfo.getEndDate());
					System.out.println();
				}
				if (reportRequestInfo.isSetScheduled()) {
					System.out.print("                Scheduled");
					System.out.println();
					System.out.print("                    " + reportRequestInfo.isScheduled());
					System.out.println();
				}
				if (reportRequestInfo.isSetSubmittedDate()) {
					System.out.print("                SubmittedDate");
					System.out.println();
					System.out.print("                    " + reportRequestInfo.getSubmittedDate());
					System.out.println();
				}
				if (reportRequestInfo.isSetCompletedDate()) {
					System.out.print("                CompletedDate");
					System.out.println();
					System.out.print("                    " + reportRequestInfo.getCompletedDate());
					System.out.println();
				}
				if (reportRequestInfo.isSetReportProcessingStatus()) {
					System.out.print("                ReportProcessingStatus");
					System.out.println();
					System.out.print("                    " + reportRequestInfo.getReportProcessingStatus());
					System.out.println();
				}
				if (reportRequestInfo.isSetGeneratedReportId()) {
					System.out.print("                GeneratedReportId");
					System.out.println();
					System.out.print("                    " + reportRequestInfo.getGeneratedReportId());
					System.out.println();
				}
			}
		}
		if (response.isSetResponseMetadata()) {
			System.out.print("        ResponseMetadata");
			System.out.println();
			ResponseMetadata responseMetadata = response.getResponseMetadata();
			if (responseMetadata.isSetRequestId()) {
				System.out.print("            RequestId");
				System.out.println();
				System.out.print("                " + responseMetadata.getRequestId());
				System.out.println();
			}
		}
		System.out.println();
		System.out.println(response.getResponseHeaderMetadata());
		System.out.println();
	}
}
